<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
<!--    <link rel="stylesheet" href="/component/layui/css/layui.css" />-->
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">权限名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="permission-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="permission-table" lay-filter="permission-table"></table>
        </div>
    </div>

    <script type="text/html" id="permission-toolbar">
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>

    <script type="text/html" id="permission-bar">
        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </script>

    <script>
        layui.use(['table', 'form', 'jquery', 'layer'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let layer = layui.layer;

            let cols = [
                [{
                        type: 'checkbox'
                    },
                    {
                        title: 'ID',
                        field: 'id',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '权限名称',
                        field: 'name',
                        align: 'center'
                    },
                    {
                        title: '权限编码',
                        field: 'code',
                        align: 'center'
                    },
                    {
                        title: '状态',
                        field: 'status',
                        align: 'center',
                        templet: function(d) {
                            if (d.status == 1) {
                                return '<span class="layui-badge layui-bg-green">正常</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-red">禁用</span>';
                            }
                        }
                    },
                    {
                        title: '创建时间',
                        field: 'create_time',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        toolbar: '#permission-bar',
                        align: 'center',
                        width: 130
                    }
                ]
            ]

            table.render({
                elem: '#permission-table',
                url: '/api/permission/list',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#permission-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(permission-table)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(permission-table)', function(obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            form.on('submit(permission-query)', function(data) {
                table.reload('permission-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function() {
                layer.open({
                    type: 2,
                    title: '新增权限',
                    shade: 0.1,
                    area: ['550px', '500px'],
                    content: '/permission/add'
                });
            }

            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    title: '修改权限',
                    shade: 0.1,
                    area: ['550px', '500px'],
                    content: '/permission/edit?id=' + obj.data['id']
                });
            }

            window.remove = function(obj) {
                layer.confirm('确定要删除该权限', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/api/permission/delete/" + obj.data['id'],
                        dataType: 'json',
                        type: 'delete',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                layer.msg(result.msg, {icon: 1}, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(result.msg, {icon: 2});
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                layer.confirm('确定要删除这些权限', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/api/permission/batchDelete",
                        data: JSON.stringify(ids),
                        dataType: 'json',
                        contentType: 'application/json',
                        type: 'delete',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                layer.msg(result.msg, {icon: 1}, function() {
                                    table.reload('permission-table');
                                });
                            } else {
                                layer.msg(result.msg, {icon: 2});
                            }
                        }
                    })
                });
            }

            window.refresh = function() {
                table.reload('permission-table');
            }
        });
    </script>
</body>
</html> 